package com.moleq.mgpos.widget;


import com.moleq.mgpos.activities.R;

import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;


public class SaleListItemControlPanel extends LinearLayout {
	
	private final static int MOVE_WIDTH = 50;
	private LinearLayout panelContainer;
	private int mRightMargin = 0;
	private Context mContext;
	private boolean mIsScrolling=false;
	private LayoutInflater mInflater;
	
	public SaleListItemControlPanel(Context context, View otherView, int width,
			int height) {
		super(context);
		this.mContext = context;

		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View controlsView = mInflater.inflate(R.layout.sale_list_item_controls,null);

//		// 改变Panel附近组件的属性
//		LayoutParams otherLP = (LayoutParams) otherView.getLayoutParams();
//		otherLP.weight = 1;
//		otherView.setLayoutParams(otherLP);

		// 设置Panel本身的属性
		LayoutParams lp = new LayoutParams(width, height);
		lp.rightMargin = -lp.width;
		mRightMargin = Math.abs(lp.rightMargin);
		this.setLayoutParams(lp);
		this.setOrientation(LinearLayout.HORIZONTAL);

		// 设置Container的属性
//		panelContainer = new LinearLayout(context);
//		panelContainer.setLayoutParams(new LayoutParams(
//				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
//	
//		this.addView(panelContainer);
//		
//		this.fillPanelContainer(controlsView);
		this.addView(controlsView);
		
		this.ClosePanel();

	}
	
	
	public void OpenPanel(){
//			new AsynMove().execute(new Integer[] { MOVE_WIDTH });// 正数展开
		
		updateControlPanel(MOVE_WIDTH);
	}
	
	public void ClosePanel(){
//			new AsynMove().execute(new Integer[] { -MOVE_WIDTH });// 负数收缩
		updateControlPanel(-MOVE_WIDTH);
	}
	
	
	private void updateControlPanel(int moveWidth){
		LayoutParams lp = (LayoutParams) SaleListItemControlPanel.this.getLayoutParams();
		if (moveWidth < 0)
			lp.rightMargin = Math.max(lp.rightMargin + moveWidth,(-mRightMargin));
		else
			lp.rightMargin = Math.min(lp.rightMargin + moveWidth, 0);

		SaleListItemControlPanel.this.setLayoutParams(lp);
	}

	
	/**
	 * 把View放在Panel的Container
	 * @param v
	 */
	public void fillPanelContainer(View v)
	{
		panelContainer.addView(v);
	}
	
	/**
	 * 异步移动Panel
	 * @author  
	 */
	class AsynMove extends AsyncTask<Integer, Integer, Void> {

		@Override
		protected Void doInBackground(Integer... params) {
			int times;
			if (mRightMargin % Math.abs(params[0]) == 0)// 整除
				times = mRightMargin / Math.abs(params[0]);
			else
				// 有余数
				times = mRightMargin / Math.abs(params[0]) + 1;

			for (int i = 0; i < times; i++) {
				publishProgress(params);
				/*
				try {
					Thread.sleep(Math.abs(params[0]));
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				*/
			}
			return null;
		}

		@Override
		protected void onProgressUpdate(Integer... params) {
			LayoutParams lp = (LayoutParams) SaleListItemControlPanel.this.getLayoutParams();
			if (params[0] < 0)
				lp.rightMargin = Math.max(lp.rightMargin + params[0],
						(-mRightMargin));
			else
				lp.rightMargin = Math.min(lp.rightMargin + params[0], 0);

			SaleListItemControlPanel.this.setLayoutParams(lp);
		}
	}

	

	

}
